import React, { useState } from 'react';
import { Switch, Redirect } from 'react-router-dom'

// http://39.96.84.223:4567/verify/adminlogin
const Login = () => {
    const [val, setVal] = useState('')
    const [val1, setVal1] = useState('')
    const [res, setRes] = useState('')
    const login = () => {
        fetch('http://39.96.84.223:4567/verify/adminlogin', {
            method: 'post',
            body: JSON.stringify({ username: val, passwd: val1 })
        })
            .then(res => res.json())
            .then(res => {
                localStorage.setItem('username', val);
                localStorage.setItem('passwd', val1);
            
                setRes(<Redirect to={{ pathname: '/' }} />)
            })
            .catch(res => {
                setRes(<h1>登录失败</h1>)
            })
        // console.log(localStorage);
    }
    return (
        <div>
            <input placeholder='请输入账号' type="text" onChange={(e) => setVal(e.target.value)} /> <br /><br />
            <input placeholder='请输入密码' type="password" onChange={(e) => setVal1(e.target.value)} /> <br /><br />
            <button onClick={login}>登录</button>
            <Switch>
                {res}
            </Switch>
        </div>
    );
}
export default Login;